﻿/*
 * LegendChildNodes
 * 
 * initial implementation : 20.10.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections;
using System.Collections.Generic;
using Petra.Gis.RenderingServices.PlanarBase.Support;


namespace Petra.Gis.RenderingServices.PlanarBase
{



  /// <summary>
  /// represents readonly enumerable collection of child nodes
  /// <para>
  /// !!!! this class must be inherited from IList<T> not from List<T> !!!
  /// reason is that add, remove, insert methods can't be overriden in List<T> !!!
  /// this is workaround.
  /// </para>
  /// </summary>
  public class LegendChildNodes : ListPlus<LegendNodeBase>
  {


    private LeafLegendNode _ownerNode;



    #region ctor


    //internal LegendChildNodes()
    //{
    //  _childs = new List<LegendNodeBase>();
    //}




    /// <summary>
    /// internal prevents users from create multiple legendChildNodes
    /// from one LeafLegendNode
    /// </summary>
    /// <param name="parent"></param>
    internal LegendChildNodes(LeafLegendNode parent, List<LegendNodeBase> nodeStorage)
      : base(nodeStorage)
    {
      _ownerNode = parent;
      //_childs = list;
    }



    #endregion ctor



    //private DisplayOrder _displayOrder;

    ///// <summary>
    ///// sets reference to <c>DisplayOrder</c> list
    ///// </summary>
    //public DisplayOrder DisplayOrder
    //{
    //  set
    //  {
    //    if (value == null)
    //      throw new ArgumentException("LegendChildNodes.DisplayOrder invalid, null");

    //    _displayOrder = value;
    //  }
    //}




    

    ///// <summary>
    ///// sets parent of current <c>LegendChildNodes</c> list
    ///// </summary>
    //public LeafLegendNode Parent
    //{
    //  set
    //  {
    //    if (value == null)
    //      throw new ArgumentException("LegendChildNodes.Parent invalid, null");

    //    //if parent changed....
    //    if (_parent != null && !_parent.Equals(value))
    //      throw new NotImplementedException("sorry not avail yet");

    //    _parent = value;
    //  }
    //}





    /// <summary>
    /// Adds an <c>LegendNodeBase</c> to the end of the <c>LegendChildNodes</c> list
    /// </summary>
    /// <param name="item">The <c>LegendNodeBase</c> to be added to the end of the list</param>
    public override void Add(LegendNodeBase newChild)
    {
      // if has already defined parent node and it is not this.parent
      // must remove first
      if (newChild.ParentNode != null && (newChild.ParentNode != _ownerNode))
        // reinsert existing node into another place
        // display order remains same
        // so must use removeNode instead of RemoveNode
        ((LeafLegendNode)newChild.ParentNode).RemoveChild(newChild, false);
      else
        // new node insert, put into displayOrder collection
        if(newChild is DataLegendNodeBase)
          _ownerNode.Legend.DisplayOrder.Insert(0, (DataLegendNodeBase)newChild);

      //add to current collection
      base.Add(newChild);
      //newChild.ParentNode = _ownerNode;

      //if(newChild is LeafLegendNode)
      //  ((LeafLegendNode)newChild).

      //_childs.Add(item);
    }





    /// <summary>
    /// Removes all elements from the <c>DisplayOrder</c> list
    /// </summary>
    public override void Clear()
    {
      //_childs.Clear();
      throw new NotImplementedException();
    }





    ///// <summary>
    ///// Determines whether an element is in the list
    ///// </summary>
    ///// <param name="item">The object to locate in the list.
    ///// </param>
    ///// <returns>true if item is found in the list, otherwise false.</returns>
    //public bool Contains(LegendNodeBase item)
    //{
    //  return _childs.Contains(item);
    //}





    /// <summary>
    /// <c>LegendChildNodes</c> list is readonly.
    /// </summary>
    public override bool IsReadOnly
    {
      get { return true; }
    }





    ///// <summary>
    ///// Removes the first occurrence of a specific <c>LegendNodeBase</c> from the list
    ///// </summary>
    ///// <param name="item">The <c>LegendNodeBase</c> to remove from the list.</param>
    ///// <returns>true if item is successfully removed; otherwise, false.
    ///// This method also returns false if item was not found in the list</returns>
    //public bool Remove(LegendNodeBase item)
    //{
    //  return Remove(item, true);
    //}





    /// <summary>
    /// Removes the first occurrence of a specific <c>LegendNodeBase</c>
    /// from the list and from <c>DisplayOrder</c> list, when indicated.
    /// </summary>
    /// <param name="item">The <c>LegendNodeBase</c> to remove from the list.</param>
    /// <param name="removeFromDisplayOrder">remove item also from <c>DisplayOrder</c> list</param>
    /// <returns>true if item is successfully removed; otherwise, false.
    /// This method also returns false if item was not found in the list</returns>
    public bool Remove(LegendNodeBase item, bool removeFromDisplayOrder)
    {
      return (base.Remove(item) && _ownerNode.Legend.DisplayOrder.Remove(item));
    }





    /// <summary>
    /// Removes the element at the specified index of the list.
    /// </summary>
    /// <param name="index">The zero-based index of the element to remove.</param>
    public override void RemoveAt(int index)
    {
      throw new NotImplementedException();
    }





    ///// <summary>
    ///// Gets the number of elements actually contained in the list
    ///// </summary>
    //public int Count
    //{
    //  get { return _childs.Count; }
    //}






    /// <summary>
    /// Copies the elements of the list 
    /// to an System.Array, starting at a particular System.Array index.
    /// </summary>
    /// <param name="array">The one-dimensional System.Array that is the destination
    /// of the elements copied from System.Collections.ICollection.
    /// The System.Array must have zero-based indexing.</param>
    /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
    public override void CopyTo(LegendNodeBase[] array, int arrayIndex)
    {
      throw new NotImplementedException();
    }





    ///// <summary>
    ///// Returns an enumerator that iterates through the list
    ///// </summary>
    ///// <returns>An enumerator for the list.</returns>
    //public IEnumerator<LegendNodeBase> GetEnumerator()
    //{
    //  return _childs.GetEnumerator();
    //}





    //IEnumerator IEnumerable.GetEnumerator()
    //{
    //  return GetEnumerator();
    //}





    /// <summary>
    /// unsupported in this version
    /// </summary>
    /// <param name="nodeIndex"></param>
    /// <param name="newPositionIndex"></param>
    public void MoveNode(int nodeIndex, int newPositionIndex)
    {
      throw new NotImplementedException();
    }





    ///// <summary>
    ///// Gets or sets the element at the specified index.
    ///// </summary>
    ///// <param name="index">The zero-based index of the element to get or set.</param>
    ///// <returns>The <c>LegendNodeBase</c> at the specified index.</returns>
    //public LegendNodeBase this[int index]
    //{
    //  get { return _childs[index]; }
    //  set { _childs[index] = value; }
    //}





    ///// <summary>
    ///// Searches for the specified object and returns the zero-based index
    ///// of the first occurrence within the entire list.
    ///// </summary>
    ///// <param name="item"> The <c>LegendNodeBase</c> to locate in the list</param>
    ///// <returns>The zero-based index of the first occurrence of <c>LegendNodeBase</c>
    ///// within the entire list.</returns>
    //public int IndexOf(LegendNodeBase item)
    //{
    //  return _childs.IndexOf(item);
    //}






    /// <summary>
    /// Inserts an <c>LegendNodeBase</c> into the list at the specified index.
    /// </summary>
    /// <param name="index">The zero-based index at which item should be inserted.</param>
    /// <param name="item">The <c>LegendNodeBase</c> to insert.</param>
    public override void Insert(int index, LegendNodeBase item)
    {
      //_childs.Insert(index, item);
      throw new NotImplementedException();
    }


  }

}
